﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WizzardTheGame
{
    class Player: Character
    {
        private static Vector3 WEAPON_OFFSET = new Vector3(10f, 23f, -5f);
        private static Vector3 ARMOR_OFFSET = new Vector3(0f, 8f, -15f);
        private const float MAX_RUNSPEED = 3;
        private Bar hpbar;
        private Weapon equippedWeapon;
        private Armor equippedArmor;
        private int level;
        private PlayerState currentPlayerState;
        private bool attacking;

        public enum PlayerState
        {
            walking,
            jumping,
            stopped,
            stopping
        }

        #region Properties

        public Bar Hpbar
        {
            get { return hpbar; }
            set { hpbar = value; }
        }

        public override Vector3 Position
        {
            get { return base.Position; }
            set
            {
                base.Position = value;
                if (EquippedWeapon != null) EquippedWeapon.Position = value + WEAPON_OFFSET;
                if (EquippedArmor != null) EquippedArmor.Position = value + ARMOR_OFFSET;
            }
        }

        public PlayerState CurrentPlayerState
        {
            get { return currentPlayerState; }
            set { currentPlayerState = value; }
        }

        public bool Attacking
        {
            get { return attacking; }
            set { attacking = value; }
        }

        public Weapon EquippedWeapon
        {
            get { return equippedWeapon; }
            set { equippedWeapon = value; }
        }

        public Armor EquippedArmor
        {
            get { return equippedArmor; }
            set { equippedArmor = value; }
        }

        public int Level
        {
            get { return level; }
            set { level = value; }
        }

        public override int Experience
        {
            get { return base.Experience; }
            set { base.Experience = value; }
        }

        #endregion

        public Player(string path)
            :base(path)
        {
            CharModel.Rotation = new Vector3(-MathHelper.PiOver2, -MathHelper.PiOver2, 0);
            CharModel.Scale = 1f;
            CurrentPlayerState = PlayerState.stopped;
            Level = 0;
        }

        private bool CheckLvlUp()
        {
            throw new NotImplementedException();
        }

        private void LvlUp()
        {
        }

        private void MoveRight()
        {
            Vector3 vec = Velocity;

            if (vec.X > -MAX_RUNSPEED)
            {
                vec.X += -1f * Speed;
                Velocity = vec;
            }
            else
            {
                vec.X = -MAX_RUNSPEED;
                Velocity = vec;
            }
            if (CharModel.Rotation.Y > 0)
            {
                vec = CharModel.Rotation;
                vec.Y *= -1;
                CharModel.Rotation = vec;
            }
             CurrentPlayerState = PlayerState.walking;
            
            }

        private void MoveLeft()
        {


            Vector3 vec = Velocity;

            if (vec.X > MAX_RUNSPEED)
            {
                vec.X += 1f * Speed;
                Velocity = vec;
            }
            else
            {
                vec.X = MAX_RUNSPEED;
                Velocity = vec;
            }
            if (CharModel.Rotation.Y < 0)
            {
                vec = CharModel.Rotation;
                vec.Y *= -1;
                CharModel.Rotation = vec;
            }
            CurrentPlayerState = PlayerState.walking;
        }

        private void Stop()
        {
            Vector3 vec = Velocity;


            if ((Velocity.X < 0) && (CurrentPlayerState == PlayerState.stopping))
            {
                vec.X += 0.1f;
                CurrentPlayerState = PlayerState.stopping;
                Console.WriteLine("a");
            }
            if ((Velocity.X > 0)&&(CurrentPlayerState == PlayerState.stopping))
            {
                vec.X -= 0.1f;
                CurrentPlayerState = PlayerState.stopping;
                Console.WriteLine("b");
            }
            if ((Velocity.X < 0.1f) && (Velocity.X > -0.1f))
            {
                vec.X = 0f;
                CurrentPlayerState = PlayerState.stopped;
            }
            else
            {
                if (!(CurrentPlayerState == PlayerState.jumping))
                {
                    CurrentPlayerState = PlayerState.stopping;
                }
            }
            Velocity = vec;
        }

        private void Jump()
        {
            Vector3 vec = Velocity;
            vec.Y = 5.001f;
            Velocity = vec;
            CurrentPlayerState = PlayerState.jumping;
        }

        protected override void Attack()
        {
        }

        private void CycleWeapon()
        {
        }

        private void CycleArmor()
        {
        }

        public void PickUp(Item item)
        {
        }

        public override void Update()
        {
            KeyboardState keyboardstate = Keyboard.GetState();

            if (keyboardstate.IsKeyDown(Keys.Right))
            {
                MoveRight();
            }
            if (keyboardstate.IsKeyDown(Keys.Left))
            {
                MoveLeft();
            }
            if(!(keyboardstate.IsKeyDown(Keys.Left)||keyboardstate.IsKeyDown(Keys.Right)))
            {
                Stop();
            }
            if (keyboardstate.IsKeyDown(Keys.Space) && Velocity.Y == 0)
            {
                Jump();
            }
            base.Update();
        }

        public override void Draw()
        {
            base.Draw();
            if (EquippedWeapon != null) EquippedWeapon.Draw();
            if (EquippedArmor != null) EquippedArmor.Draw();
        }

        protected override void ApplyGravity()
        {
            if (CurrentPlayerState == PlayerState.jumping)
            {
                Vector3 vec = Velocity;
                vec.Y -= GRAVITY;
                Velocity = vec;
                if (Velocity.Y < 0)
                {
                    CurrentPlayerState = PlayerState.stopped;
                }
            }
            else
            {
                base.ApplyGravity();
            }
        }
    }
}
